<?php
/**
 * Home controller
 * @author Vanadia
 */
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Default controller class
 */
class Login extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library('session');
		$this->load->helper('url');
		$this->load->model('admin_model');
		
	}
	
	
	public function index() {
	    $method = $_SERVER['REQUEST_METHOD'];//判断是否传值过来
	    $msg = '';
	    if(strtolower($method) == 'post'){
    	    //取用户名和密码
    	    $uname = $this->input->post('uname');
    	    $passwd = $this->input->post('passwd');
    	    $res = $this->admin_model->getAdminByUname($uname);
    	    if ($res){
    	        //存在 判断密码是否正确
    	        if (md5(md5($passwd)) == $res['password']){
    	            $this->session->set_userdata('admin_uid', $res['id']);
    	            $this->session->set_userdata('admin_name',$res['name']);
    	            redirect(site_url('admin/user_management'));//正确 跳转首页    	             
    	        }else {
    	            $msg= '密码错误!';//报错 跳转登陆
    	        }
    	    } else{
    	    	$msg = '用户名错误!';
    	    }
	    }	    
	   //塞入msg
	   $this->load->view('admin/login',array('msg'=>$msg));
	}
	
	public function logout(){
		$uid = $this->session->userdata('admin_uid');
		if(!empty($uid)){
			$this->session->unset_userdata('admin_uid');
			$this->session->unset_userdata('admin_uname');
			session_destroy();
			session_start();
		}
		redirect(site_url('admin'));
	}
}